#!/usr/bin/env python

# Copyright 2011 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Street Identity Demo Relying Party Setup Handlers."""

import os

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from django.utils import simplejson as json

import logging

from lib import authn #@UnresolvedImport
from lib import utils #@UnresolvedImport @UnusedImport
from lib.loggable import loggable #@UnresolvedImport
from lib import oauth2client #@UnresolvedImport @UnusedImport
from lib import http #@UnresolvedImport
from lib.error import handle_error #@UnresolvedImport
import app_params as application_params #@UnresolvedImport
import model
import account #@UnusedImport

__author__ = "Maciej Machulak"
__maintainer__ = "Maciej Machulak"
__email__ = "mmachulak@google.com"

__copyright__ = "Copyright 2011, Google Inc."
__license__ = "Apache License 2.0"
__version__ = "0.1"
__status__ = "Prototype"

class SetupHandler(webapp.RequestHandler):

    @authn.login_required
    @loggable('account setup page')  
    def get(self):
          
      request_param = self.request.get('request')    
      logging.debug('request param %s' % request_param)    
          
      values = {
          'user': self.user,
          'request_param': request_param
      }
      path = os.path.join(os.path.dirname(__file__), 'templates/setup.html')
      self.response.out.write(template.render(path, values))
      
class GetAddressHandler(webapp.RequestHandler):

    @authn.login_required
    @loggable('get address for the user')
    def post(self):
      
      address_id = self.request.get('address_id')
      
      headers = {}
      params = {}
      ap_url = application_params.APConfig.ap
      street_address_path = application_params.APConfig.street_address_path
      response = http.get_request(ap_url, street_address_path + address_id , headers, params)                
      if response.status != 200:
        logging.error('invalid street address response from the server')
        handle_error(self)
        return              
      response_read = response.read()
      logging.debug('got reply from the server: %s' % response_read)

      json_response = json.loads(response_read)
      logging.debug('parsed response: %s' % json_response)

      logging.debug('storing address for the user')
      address = model.Address(
            address = json_response['address'],
            type = json_response['type'],
            provider = json_response['provider'],
            user = self.user)
      address.put()
      logging.debug('address stored in a database')

      values = {
          'user': self.user,
          'address' : address
      }
      logging.debug('forwarding user to the account view')
      path = os.path.join(os.path.dirname(__file__), 'templates/account.html')
      self.response.out.write(template.render(path, values))   
        